package edu.iscas.xcheck.analysis.dom.utils;

import org.apache.commons.lang3.ArrayUtils;

import edu.iscas.xcheck.analysis.dom.model.DomNode;

public class LayoutUtils {
	public static final String[] tagsIgnore = { "A", "AREA", "B", "BLOCKQUOTE",
		"BR", "CANVAS", "CENTER", "CSACTIONDICT", "CSSCRIPTDICT", "CUFON",
		"CUFONTEXT", "DD", "EM", "EMBED", "FIELDSET", "FONT", "FORM",
		"HEAD", "HR", "I", "LABEL", "LEGEND", "LINK", "MAP", "MENUMACHINE",
		"META", "NOFRAMES", "NOSCRIPT", "OBJECT", "OPTGROUP", "OPTION",
		"PARAM", "S", "SCRIPT", "SMALL", "SPAN", "STRIKE", "STRONG",
		"STYLE", "TBODY", "TITLE"
		,"TR"//, "TD"//
		, "TT", "U"
		, "UL" 
		};
	
	public static boolean isLayoutNode(DomNode node) {
		if(node.isText()){
			return false;
		}
		// is it ignored?
		if (isIgnored(node)) {
			return false;
		}
		if(node.isVisible()==0){
			return false;
		}
		// check size
		int[] c = node.getCoords();
		if (c == null) {
			return false;
		}
		if (c[0] < 0 || c[1] < 0 || c[2] <= 0 || c[3] <= 0) {
			return false;
		}
		int t = 5; // 5 (Negligible dimensions)
		if (c[2] - c[0] <= t || c[3] - c[1] <= t) {
			return false;
		}

		return true;
	}

	private static boolean isIgnored(DomNode node) {
		if (ArrayUtils.contains(tagsIgnore, node.getTagName())) {
			return true;
		}
		return false;
	}

}
